package me.firstapp.spi.bbs.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.ListObject;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.bbs.JsonSection;
import me.firstapp.spi.base.impl.BaseServiceImpl;
import me.firstapp.spi.bbs.SectionService;

@Service
public class SectionServiceImpl extends BaseServiceImpl implements SectionService {

	@Override
	public ListObject<JsonSection> findSectionList() {
		ListObject<JsonSection> result = new ListObject<JsonSection>();
		Map<String, Object> parameters = new HashMap<>();
		JSONObject jsonObject = api(parameters, "/section/getAllSections.do", HttpMethod.GET, "查询板块");

		try {
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JSONArray jsonArray = jsonObject.getJSONArray("items");
				List<JsonSection> sections = new ArrayList<JsonSection>();
				if (jsonArray != null) {
					for (int i = 0; i < jsonArray.size(); i++) {
						JSONObject _jsonObject = jsonArray.getJSONObject(i);
						if (_jsonObject != null) {
							JsonSection section = JSONObject.toJavaObject(_jsonObject, JsonSection.class);
							sections.add(section);
						}
					}
				}
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setItems(sections);
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public PageObject<JsonSection> findSectionPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType) {
		PageObject<JsonSection> result = new PageObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("sortParam", sortParam);
			parameters.put("sortType", sortType);
			JSONObject jsonObject = apiBody(parameters, "/section/findSectionPage.do", "分页查询板块", searchParams);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JSONObject pageJsonObject = jsonObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonSection> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonSection> sectionList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonSection section = JSONObject.toJavaObject(_jsonObject, JsonSection.class);
								sectionList.add(section);
							}
						}
					}
					jsonPage.setList(sectionList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonSection> addSection(String name, String description, Integer sort) {
		SingleObject<JsonSection> result = new SingleObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("name", name);
			parameters.put("description", description);
			parameters.put("sort", sort);

			JSONObject resultJson = api(parameters, "/section/addSection.do", HttpMethod.POST, "新增板块");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JsonSection apiJsonSection = JSONObject.toJavaObject(resultJson.getJSONObject("object"),
						JsonSection.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(apiJsonSection);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Object> deleteSections(String ids) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("sectionIds", ids);

			JSONObject resultJson = api(parameters, "/section/deleteSections.do", HttpMethod.POST, "删除板块");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonSection> updateSection(Long id, String name, String description, Integer sort) {
		SingleObject<JsonSection> result = new SingleObject<JsonSection>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("id", id);
			parameters.put("name", name);
			parameters.put("description", description);
			parameters.put("sort", sort);

			JSONObject resultJson = api(parameters, "/section/updateSection.do", HttpMethod.POST, "修改板块");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JsonSection apiJsonSection = JSONObject.toJavaObject(resultJson.getJSONObject("object"),
						JsonSection.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(apiJsonSection);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonSection> findById(Long id) {
		SingleObject<JsonSection> result = new SingleObject<JsonSection>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("id", id);

			JSONObject resultJson = api(parameters, "/section/findSectionById.do", HttpMethod.GET, "通過id查詢板块");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JsonSection apiJsonSection = JSONObject.toJavaObject(resultJson.getJSONObject("object"),
						JsonSection.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(apiJsonSection);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Object> updateTopicAndReplyCounts(Long apiClientId) {
		SingleObject<Object> result = new SingleObject<Object>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("apiClientId", apiClientId);

			JSONObject resultJson = api(parameters, "/batch/updateTopicAndReplyCounts.do", HttpMethod.POST,
					"批量更新板块话题数量和回复数量");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Object> updateSectionCache() {
		SingleObject<Object> result = new SingleObject<Object>();
		try {
			JSONObject resultJson = api(null, "/section/updateSectionCache.do", HttpMethod.POST, "更新板块缓存");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

}
